package com.coai.user.repository

import com.coai.user.model.User
import com.coai.user.model.UserStatus
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.util.Optional

@Repository
interface UserRepository : JpaRepository<User, String> {
    
    fun findByUsername(username: String): Optional<User>
    
    fun findByEmail(email: String): Optional<User>
    
    fun existsByUsername(username: String): Boolean
    
    fun existsByEmail(email: String): Boolean
    
    fun findByStatus(status: UserStatus): List<User>
    
    @Modifying
    @Query("UPDATE User u SET u.status = :status WHERE u.id = :userId")
    fun updateUserStatus(userId: String, status: UserStatus)
    
    @Query("SELECT u FROM User u WHERE u.enabled = true AND u.status != 'OFFLINE'")
    fun findActiveUsers(): List<User>
}
